package com.qing.thread.lookstate;

/**
 * 观察线程的状态
 * 1：NEW 就绪状态
 * 2：RUNNABLE 运行状态
 * 3：TIMED_WAITING 阻塞
 * 4：TERMINATED terminated 终止状态
 */
public class LookThreadState {
    public static void main(String[] args) throws InterruptedException {
        Thread thread = new Thread(() -> {
            for (int i = 0; i < 5; i++) {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            System.out.println("---------------------");
        });
        //获取线程状态
        //创建好的状态
        System.out.println(thread.getState());

        //启动后的状态
        thread.start();
        System.out.println(thread.getState());
        //线程不终止，则一秒输出一次状态
        while (thread.getState() != Thread.State.TERMINATED) {
            Thread.sleep(1000);
            System.out.println(thread.getState());
        }
        // thread.start();线程一旦结束，TERMINATED状态，就不能再启动了，报错
    }
}
